package com.dozen.commonbase.view.cube;

import android.content.Context;
import android.graphics.PixelFormat;
import android.opengl.GLSurfaceView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;

/**
 * @author: Dozen
 * @description:
 * @time: 2021/11/9
 */
public class CubeSimpleView extends GLSurfaceView {

    private Context context;
    public CubeRenderer mRender;

    public CubeSimpleView(Context context) {
        super(context);
        this.context = context;
        init();
    }

    public CubeSimpleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
    }

    private void init() {

        setEGLConfigChooser(8, 8, 8, 8, 16, 0);
        getHolder().setFormat(PixelFormat.TRANSLUCENT);
        setZOrderOnTop(true);

        // Request an OpenGL ES 2.0 compatible context.
        setEGLContextClientVersion(2);
        mRender = new CubeRenderer(context, new CubeBean());
        // Set the renderer to our demo renderer, defined below.
        setRenderer(mRender);

        // render模式为只在绘制数据发生改变时才绘制view,此设置会阻止绘制GLSurfaceView的帧，直到你调用了requestRender().
        setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);

        //以下模式会不断绘制，我的手机上大概5--6 毫秒绘制一次
        //setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);

    }

    private float mPreviousX = 0, mPreviousY = 0;

    public boolean onTouchEvent(final MotionEvent e) {
        queueEvent(new Runnable() {
            @Override
            public void run() {
                float x = e.getX();
                float y = e.getY();
                switch (e.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        break;
                    case MotionEvent.ACTION_MOVE:
                        float dx = y - mPreviousY;
                        float dy = x - mPreviousX;
//                        mRender.mAngleX += dx;
                        mRender.mAngleY += dy;
                        requestRender();
                        Log.d("Dozen", "1111111111:dx=" + mRender.mAngleX + "_____dy=" + mRender.mAngleY);
                        break;
                }
                mPreviousX = x;
                mPreviousY = y;
            }
        });
        return true;
    }
}
